﻿Imports System.Xml

Module Module1

    Sub Main()

        'wtbooks_1()
        'wtbooks_2()
        'rdbooks_1()

        Dim intData(30)
        Dim strArr(30)
        strArr = {"ABC", "BCD"}
        intData = {1, 2, 3, 4, 5, 6}
        For Each x In intData
            Console.WriteLine(x)
        Next
        For Each x In strArr
            Console.WriteLine(x)
        Next

        Dim al As ArrayList
        al = New ArrayList
        al.Add(intData)
        al.Add(strArr)
        al.Add(5)

        For Each a In al
            Console.WriteLine(a)
        Next

        Dim ht As Hashtable = New Hashtable

        ht.Add("a", "aaa")
        ht.Add("b", 5)

        For Each x In ht.Keys
            Console.WriteLine(x)
        Next

        For Each x In ht.Values
            Console.WriteLine(x)
        Next

        Console.ReadKey()
    End Sub


    ' 编写xml 的方法一
    Sub wtbooks_1()
        Dim path As String = My.Application.Info.DirectoryPath & "\..\..\data\books2.xml"

        Dim xws As XmlWriterSettings = New XmlWriterSettings()
        xws.Indent = True
        xws.NewLineOnAttributes = True
        Dim xw As XmlWriter = XmlWriter.Create(path, xws)

        xw.WriteStartDocument()
        xw.WriteStartElement("Authors")

        xw.WriteStartElement("author")
        xw.WriteAttributeString("code", "1")
        xw.WriteElementString("fname", "Zara")
        xw.WriteElementString("lname", "Ali")
        xw.WriteEndElement()


        xw.WriteStartElement("author")
        xw.WriteAttributeString("code", "2")
        xw.WriteElementString("fname", "Priya")
        xw.WriteElementString("lname", "Sharma")
        xw.WriteEndElement()


        xw.WriteStartElement("author")
        xw.WriteAttributeString("code", "3")
        xw.WriteElementString("fname", "Anshuman")
        xw.WriteElementString("lname", "Mohan")
        xw.WriteEndElement()


        xw.WriteStartElement("author")
        xw.WriteAttributeString("code", "4")
        xw.WriteElementString("fname", "Bibhuti")
        xw.WriteElementString("lname", "Banerjee")
        xw.WriteEndElement()

        xw.WriteStartElement("author")
        xw.WriteAttributeString("code", "5")
        xw.WriteElementString("fname", "Riyan")
        xw.WriteElementString("lname", "Sengupta")
        xw.WriteEndElement()

        xw.WriteEndElement()
        xw.WriteEndDocument()
        xw.Flush()
        xw.Close()
    End Sub


    ' 写入xml 方法二
    Sub wtbooks_2()
        Dim path As String = My.Application.Info.DirectoryPath & "\..\..\data\books2.xml"
        Dim xd As XmlDocument = New XmlDocument()
        xd.Load(path)
        Dim newAuthor As XmlElement = xd.CreateElement("author")
        newAuthor.SetAttribute("code", "6")
        Dim fn As XmlElement = xd.CreateElement("fname")
        fn.InnerText = "Bikram"
        newAuthor.AppendChild(fn)
        Dim ln As XmlElement = xd.CreateElement("lname")
        ln.InnerText = "Seth"
        newAuthor.AppendChild(ln)
        xd.DocumentElement.AppendChild(newAuthor)
        Dim tr As XmlTextWriter = New XmlTextWriter(path, Nothing)
        tr.Formatting = Formatting.Indented
        xd.WriteContentTo(tr)
        tr.Close()

    End Sub

    Sub rdbooks_1()
        Dim path = My.Application.Info.DirectoryPath & "\..\..\data\books2.xml"
        Dim xr As XmlReader = XmlReader.Create(path)
        Do While xr.Read()
            If xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "author" Then
                Console.WriteLine("{0} {1}", xr.GetAttribute(0), xr.Name)


            End If
        Loop

    End Sub


    Function createNode(name As String, publish As String, price As Double) As XNode




        Dim n As XNode = <book>
                             <name>name</name>
                             <price>price</price>
                             <publish>publish</publish>
                         </book>



        Return n

    End Function


    Sub readBook(xd As XDocument, index As Integer)
        If xd IsNot Nothing Then
            Dim els = xd.Root.Elements()
            Dim x As Integer = index - 1
            If x < 0 Then
                x = 0
            End If
            Dim e As XElement = els(x)
            Dim cels = e.Elements()
            For Each ce In cels
                Console.WriteLine("标签：{0}，值：{1}", ce.Name, ce.Value)
            Next
        End If
    End Sub

End Module
